package com.liuyang.web;

import com.alibaba.fastjson.JSON;
import com.liuyang.pojo.Brand;
import com.liuyang.pojo.PageBean;
import com.liuyang.service.BrandService;
import com.liuyang.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService service = new BrandServiceImpl();
    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、调用service查询
        List<Brand> brands = service.selectAll();

        //2、转为JSON
        String jsonString = JSON.toJSONString(brands);

        //3、写数据
        //解决乱码问题
        resp.setContentType("text/json;charset=utf-8");

        resp.getWriter().write(jsonString);
    }
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、接收品牌数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为Brand对象
        Brand brand = JSON.parseObject(params,Brand.class);

        //2、调用service添加
        service.add(brand);
        resp.getWriter().write("success");
    }
    //批量删除
    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、接收品牌数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为int
        int[] ids = JSON.parseObject(params,int[].class);

        //2、调用service添加
        service.deleteByIds(ids);
        resp.getWriter().write("success");
    }
    //删除按钮
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、接收品牌数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为int
        int id = JSON.parseObject(params,int.class);
        System.out.println(id);
        //2、调用service添加
        service.deleteById(id);

        resp.getWriter().write("success");
    }
    //修改按钮
    public void editData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、接收品牌数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为int
        int id = JSON.parseObject(params,int.class);
        System.out.println(id);
        //2、调用service添加
        List<Brand> brand = service.editData(id);
        System.out.println(brand);
        //转为JSON
        String jsonString = JSON.toJSONString(brand);

        //写数据
        //解决乱码问题
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }
    //分页查询
    public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String _currentPage = req.getParameter("currentPage");
       String _pageSize = req.getParameter("pageSize");

       int currentPage = Integer.parseInt(_currentPage);
       int pageSize = Integer.parseInt(_pageSize);

       //调用service查询
        PageBean<Brand> pageBean = service.selectByPage(currentPage,pageSize);

        //2、转为JSON
        String jsonString = JSON.toJSONString(pageBean);

        //3、写数据
        //解决乱码问题
        resp.setContentType("text/json;charset=utf-8");

        resp.getWriter().write(jsonString);
    }
    public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //获取查询条件对象
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为Brand
        Brand brand = JSON.parseObject(params,Brand.class);

        //调用service查询
        PageBean<Brand> pageBean = service.selectByPageAndCondition(currentPage,pageSize,brand);

        //2、转为JSON
        String jsonString = JSON.toJSONString(pageBean);

        //3、写数据
        //解决乱码问题
        resp.setContentType("text/json;charset=utf-8");

        resp.getWriter().write(jsonString);
    }
}
